<!--
 * @description  : 询问笔录物品添加
 * @descriptionDetail: 询问笔录物品添加
 * @copyright    : 浙江烟草
 * @author       : mc
 * @create       : 2022-07-11 14:36:53
-->
<template>
	<view class="page">
		<!-- #ifdef H5-ONLY -->
		<theme-navbar :is-white="false" :title="navTitle" :custom-back="goBack" style="z-index: 99999"></theme-navbar>
		<!-- #endif -->
		<view class="content content-bg ifn-content u-padding-30" style="background-color: white">
			<view class="sub-title-lay">
				<view class="sub-title">案件编号</view>
				<text>{{ caseCode }}</text>
			</view>
			<view class="u-flex search-lay u-row-between">
				<view class="search-title">品种及规格</view>
				<view class="u-flex">
					<view style="position: relative">
						<input style="position: absolute; top: 8px; left: 4px" class="u-input__input" type="text"
							placeholder="请输入关键字" placeholderStyle="'color: #c0c4cc;'" :focus="scanOtherShow" confirmType="done"
							:cursor-spacing="0" :show-confirm-bar="true" v-show="scanOtherShow" id="scanOther" v-model="hideVal"
							@input="scanOtherInputEvent" />
						<u-input :custom-style="customStyle" placeholder="请输入关键字" :focus="focus" :disabled="scanOtherShow"
							v-model="searchTxt" @blur="searchBlur" @focus="searchFocus"></u-input>
					</view>
					<view class="u-m-l-10">
						<u-icon name="scan" color="primary" size="52" @click="scanClick"></u-icon>
					</view>
				</view>
			</view>
			<u-line class="u-m-t-15 u-m-b-15 good-search-line" :color="$theme['u_border_color']"></u-line>
			<view class="good-search-lay" :style="{top: searchGoodTop,height: searchGoodHeight}" v-if="showSearchGoods"
				@touchstart="touchStart" @touchend="touchEnd">
				<scroll-view scroll-y="true" :style="{height: searchGoodHeight}">
					<u-cell-group>
						<u-cell-item :titleStyle="{fontSize: '32rpx',}"
							:title="item.productName + ' ' + (item.stripCode || '') + ' , ' + (getItemActiveStatus(item.isActive))"
							:arrow="true" v-for="item in searchGoodList" :key="item.productUuid"
							@click="choiceGoodItem(item)"></u-cell-item>
					</u-cell-group>
				</scroll-view>
			</view>
			<view :style="{height: getListHeight()}">
				<me-list :normalScrollView="false" :i="0" idKey="goodsRdmUuid" :initiativeTap="true" :limit="limit"
					:height="getListHeight()" :loadData="loadData" ref="mescrollRef0">

					<view slot-scope="d">
						<view class="num-lay">
							<view class="num-title">{{ getIndexByGoodsList(d.data.goodsRdmUuid) }}</view>
							<view class="num-content">{{ d.data.goodsName }}</view>
						</view>
						<view class="u-flex">
							<view class="pty-lay" style="flex-shrink: 1; flex-grow: 1">
								<view>查获数量：</view>
								<good-input type="number" class="u-m-l-20 u-m-r-20" :customStyle="cusStyle" inputAlign="right"
									placeholder="请输入" :extStr="d.data.goodsRdmUuid" v-model="d.data.qty" @input="inputQtyEvent"
									@blur="BlurQtyEvent"></good-input>
								<view>{{ d.data.unit }}</view>
							</view>
							<view class="price-lay">
								<view>查获单价：</view>
								<u-input type="number" :disabled="true" class="u-m-l-10 u-m-r-10" inputAlign="right" placeholder=""
									v-model="d.data.price"></u-input>
								<view>元/{{ d.data.unit }}</view>
							</view>
						</view>
						<view class="u-flex">
							<view class="u-flex-1 price-lay">
								<view>批发价：</view>
								<u-input type="number" :disabled="true" class="u-m-l-20 u-m-r-20" inputAlign="right" placeholder=""
									v-model="d.data.wholesalePrice"></u-input>
								<view>元</view>
							</view>
							<view class="u-flex-1 price-lay">
								<view>查获金额：</view>
								<u-input type="number" :disabled="true" class="u-m-l-20 u-m-r-20" inputAlign="right" placeholder=""
									v-model="d.data.amt"></u-input>
								<view>元</view>
							</view>
						</view>
						<view class="u-flex">
							<view class="u-flex-1 price-lay">
								<view>购进数量：</view>
								<good-input type="number" :customStyle="cusStyle" class="u-m-l-20 u-m-r-20" inputAlign="right"
									placeholder="请输入" v-model="d.data.buyQty" :extStr="d.data.goodsRdmUuid" @input="inputGqtyEvent"
									@blur="handleBlurQty"></good-input>
								<view>条</view>
							</view>
							<view class="u-flex-1 pty-lay">
								<view>购进单价 ：</view>
								<good-input type="number" class="u-m-l-20 u-m-r-20" :customStyle="cusStyle" inputAlign="right"
									placeholder="请输入" v-model="d.data.buyPrice" :extStr="d.data.goodsRdmUuid"
									@input="inputPriceEvent"></good-input>
								<view>元</view>
							</view>
						</view>
						<view class="u-flex">
							<view class="u-flex-1 price-lay" style="width: 100% !important">
								<view>违法进货金额 ：</view>
								<u-input type="number" class="u-m-l-20 u-m-r-20" :disabled="true" inputAlign="right" placeholder="请输入"
									v-model="d.data.illegalbuyamt"></u-input>
								<view>元</view>
							</view>
						</view>
						<view class="u-flex">
							<view class="u-flex-1 price-lay">
								<view>已销数量：</view>
								<good-input type="number" class="u-m-l-20 u-m-r-20" :customStyle="cusStyle" inputAlign="right"
									placeholder="请输入" :disabled="true" :extStr="d.data.goodsRdmUuid" v-model="d.data.illegalQty"
									@input="inputBuyQtyEvent"></good-input>
								<view>条</view>
							</view>
							<view class="u-flex-1 pty-lay">
								<view>销售单价 ：</view>
								<good-input type="number" class="u-m-l-20 u-m-r-20" :customStyle="cusStyle" inputAlign="right"
									placeholder="请输入" :extStr="d.data.goodsRdmUuid" v-model="d.data.illegalPrice"
									@input="inputBuyPriceEvent"></good-input>
								<view>元</view>
							</view>
						</view>
						<view class="u-flex">
							<view class="u-flex-1 price-lay">
								<view>销售金额：</view>
								<u-input type="number" :disabled="true" class="u-m-l-20 u-m-r-20" inputAlign="right" placeholder=""
									v-model="d.data.illegalAmt"></u-input>
								<view>元</view>
							</view>
							<view class="u-flex-1 price-lay">
								<view>获利金额：</view>
								<u-input type="number" :disabled="true" class="u-m-l-20 u-m-r-20" inputAlign="right" placeholder=""
									v-model="d.data.reapProfit"></u-input>
								<view>元</view>
							</view>
						</view>
						<u-form-item label-width="auto" :label-style="labelStyle" class="u-m-l-20" label="物品性质:">
							<template v-if="chooseType(d.data.goodsType)">
								<dic-input :keyWord="$u.dic.DIC_KEYS.GOODS_PROP_SUBTYPE" :dicCode="d.data.propSubtype" :border="false"
									inputAlign="right" type="select" :select-open="dicSelectOpen($u.dic.DIC_KEYS.GOODS_PROP_SUBTYPE)"
									placeholder="请选择" v-model="d.data.propSubTypeName"
									@changeInputModelData="(name)=>{$set(d.data, 'propSubTypeName', name)}"
									@click="showDic($u.dic.DIC_KEYS.GOODS_PROP_SUBTYPE,d.data.propSubtype,d.data.goodsRdmUuid)" />
							</template>
							<template v-else>
								<view style="text-align:right;width:100%;color: rgb(192, 196, 204);">不可选择</view>
							</template>
						</u-form-item>
						<view class="divider-m"></view>
					</view>
				</me-list>
			</view>
		</view>
		<view class="submit-btns btn-other-cls">
			<u-button class="u-btn" type="primary" @click="confirm">确定</u-button>
		</view>
		<dic-choice :keyWord="dicKeyWords" :currentSelect="dicCurrentSelect" v-model="dicShow"
			@confirm="selectDicConfirm"></dic-choice>
		<u-popup mode="bottom" height="50%" v-model="choiceMergeShow">
			<merge-goods-pick :dataList="choiceMergeList" @choose="choiceMerge"></merge-goods-pick>
		</u-popup>
		<u-popup mode="bottom" height="50%" v-model="choiceSingleShow">
			<m-single-choice :mDatas="singleList" @m-select="choiceSingle"></m-single-choice>
		</u-popup>
	</view>
</template>

<script>
	import mescrollList from '@/components/mescroll_list.vue';
	import DicChoice from '@/components/dic_choice.vue';
	import DicInput from '@/components/dic_input.vue';
	import caseService from '@/service/case/case.service';
	import officeService from '@/service/case/office.service';
	import mergeGoodsPick from '../components/mergeGoodsPickComponent.vue';
	import goodInput from '../components/goodInput.vue';
	import MSingleChoice from './mSingleChoice.vue';
	import {
		SHOW_CHOOSE_GOODS_TYPE
	} from '@/service/case/mapping/enum.js';
	import {
		v4 as uuidv4
	} from 'uuid';
	export default {
		components: {
			'me-list': mescrollList,
			'dic-choice': DicChoice,
			'dic-input': DicInput,
			mergeGoodsPick,
			goodInput,
			MSingleChoice,
		},
		data() {
			return {
				caseCode: '',
				caseUuid: '',
				limit: 20,
				hideVal: '',
				dicShow: false, // dic_select 是否显示
				dicKeyWords: '', // dic 关键字
				dicCurrentSelect: '', // dic 当前选择项
				currIndex: 0,
				searchTxt: '',
				labelStyle: {
					fontSize: '30rpx',
				},
				customStyle: {
					backgroundColor: this.$theme['u_bg_color'],
					borderRadius: '20px',
					paddingLeft: '10px',
					paddingRight: '10px',
				},
				cusStyle: {
					color: '#FF943E',
				},
				dataList: [],
				showSearchGoods: false,
				searchFlag: false, // 快慢问题
				searchGoodList: [],
				defaultWindowHeight: this.$u.sys().windowHeight,
				searchGoodTop: '',
				searchGoodHeight: '',
				choiceMergeList: [],
				choiceMergeShow: false,
				focus: false,
				choiceSingleShow: false,
				singleList: [],
				choiceData: undefined,
				orginalCaseClueList: [],
				showSoftInputView: false,
				tmpChoice: undefined,
				scanOtherShow: false, //  扫码枪状态
				scanOtherData: {}, // 扫码的到的数据集合
				addFrom: 'search', // 默认 search  scanBar  infraredScan
				scanVal: undefined,
			};
		},
		computed: {
			realDataList() {
				let tmpList = this.dataList.filter((item) => {
					return true;
				});
				tmpList.reverse();
				return tmpList;
			},
		},
		onLoad(opt) {
			let opo = this.getRouteParam();
			let tmpList = opo.goodsList;
			this.orginalCaseClueList = opo.clueList;
			if (tmpList) {
				tmpList = this.$u.deepClone(tmpList);
				tmpList.forEach((itm) => {
					itm.goodsRdmUuid = itm.goodsDtlUuid || uuidv4();
				});
				this.dataList = [...tmpList];
			}
			this.caseCode = opt.caseCode;
			this.caseUuid = opt.caseUuid;
		},
		watch: {
			searchTxt(newVal, oldVal) {
				this.$u.debounce(this.getSearchGoodList, 500);
			},
		},
		onUnload() {
			uni.offWindowResize(() => {
				console.log('取消监听窗口尺寸变化事件');
			});
		},
		mounted() {
			this.init();
		},
		methods: {
			chooseType(value) {
				const index = SHOW_CHOOSE_GOODS_TYPE.findIndex(item => item.value === value)
				return index >= 0;
			},
			getListHeight() {
				return this.$u.sys().windowHeight - 50 - 60 - 44 - uni.upx2px(180) - uni.upx2px(60) - this.$u.sys()
					.statusBarHeight + 'px';
			},
			getGoodSearchPopupHeight() {
				this.$u.getRect('.good-search-line').then((res) => {
					this.searchGoodTop = res.bottom + 'px';
					this.searchGoodHeight = this.$u.sys().windowHeight - res.bottom - this.$u.sys().statusBarHeight + 'px';
				});
			},
			getIndexByGoodsList(id) {
				for (let i = 0; i < this.realDataList.length; i++) {
					if (this.realDataList[i].goodsRdmUuid == id) {
						return this.realDataList.length - i;
					}
				}
				return -1;
			},
			getItemActiveStatus(isActive) {
				if (isActive == 1) {
					return '启用';
				} else if (isActive == 2) {
					return '待启用';
				} else if (isActive == 3) {
					return '待引入';
				}
				return '';
			},
			updatePreObj(obj) {
				this.preObj = {
					goodsTypeName: obj.goodsTypeName,
					propSubTypeName: this.getDicName(this.$u.dic.DIC_KEYS.GOODS_PROP_SUBTYPE, obj.propSubtype),
					propSubtype: obj.propSubtype,
					goodsType: obj.productType || obj.goodsType,
					goodsUuid: obj.productUuid || obj.goodsUuid,
					goodsName: obj.productName || obj.goodsName,
					unit: obj.unit || '',
					price: obj.price || '',
					qty: obj.qty || '',
					amt: obj.amt || '',
					buyQty: obj.buyQty || '',
					buyPrice: obj.buyPrice || '',
					illegalbuyamt: obj.illegalbuyamt || '',
					illegalQty: obj.illegalQty || '',
					illegalPrice: obj.illegalPrice || '',
					illegalAmt: obj.illegalAmt || '',
					caseProp: obj.caseProp || '',
					caseProperty: obj.caseProperty || '',
					isSearched: false,
					wholeSalePrice: obj.wholeSalePrice || '',
					reapProfit: obj.reapProfit || ''
				};
			},
			init() {
				let self = this;
				this.getGoodSearchPopupHeight();
				if (this.dataList && this.dataList.length > 0) {
					this.updatePreObj(this.dataList[this.dataList.length - 1]);
				} else {
					this.preObj = undefined;
				}
				uni.onWindowResize((res) => {
					if (this.defaultWindowHeight - res.size.windowHeight > 180) {
						// 阈值 超过这个值表示软键盘弹出了
						this.showSoftInputView = true;
					} else {
						// 否则软键盘收起了
						this.showSoftInputView = false;
						if (this.searchFlag) return;
						if (this.tmpChoice) {
							this.showSearchGoods = false;
							this.choiceGoodItem(this.tmpChoice);
							this.tmpChoice = undefined;
						}
					}
				});
			},
			choiceSingle(e) {
				this.choiceSingleShow = false;
				if (this.choiceData) {
					let obj;
					for (let i = 0; i < this.dataList.length; i++) {
						if (this.dataList[i].goodsRdmUuid == this.choiceData.goodsRdmUuid) {
							obj = this.dataList[i];
							break;
						}
					}
					if (obj) {
						this.$set(obj, 'caseProperty', e.caseProperty);
						this.$set(obj, 'caseProp', e.casepropUuid);
					}
				}
			},
			searchBlur(e) {
				setTimeout(() => {
					console.log('==searchBlur==');
					if (!this.showSoftInputView && !this.searchFlag) {
						this.showSearchGoods = false;
					}
				}, 100);
			},
			touchStart(e) {
				console.log('==touchStart==');
				this.searchFlag = true;
			},
			touchEnd(e) {
				console.log('==touchEnd==');
				this.searchFlag = false;
			},
			searchFocus(e) {
				if (!this.$u.test.isEmpty(this.searchTxt)) {
					this.searchFlag = false;
					this.showSearchGoods = true;
				}
			},
			/*
			 * 条码查询
			 * */
			scanClick() {
				// #ifdef H5-DINGDING
				this.$deviceApi
					.barCode()
					.then((res) => {
						this.$modal.showLoading('查询中...');
						caseService
							.searchByCode(res)
							.then((res1) => {
								this.$modal.hideLoading();
								if (res1.success && res1.data && res1.data.length > 0) {
									this.addFrom = 'scanBar';
									this.scanVal = res;
									this.choiceGoodItem(res1.data[0]);
								} else {
									this.$u.toast('未查询到该物品');
								}
							})
							.catch((e) => {
								this.$modal.hideLoading();
							});
					})
					.catch((err) => {
						this.$u.toast('扫描失败');
					});
				// #endif
			},
			/**
			 * 扫码枪扫码
			 * @param {Object} ev
			 * @param {Object} str
			 */
			scanOther() {
				this.scanOtherData = {};
				this.scanOtherShow = !this.scanOtherShow;
				this.$el.querySelector('#scanOther input').setAttribute('inputmode', 'none');
				this.focus = !this.scanOtherShow;
				setTimeout(() => {
					this.$el.querySelector('#scanOther input').removeAttribute('inputmode');
				}, 100);
				this.searchTxt = '';
			},
			scanOtherInputEvent(e) {
				let tmpStr = e.detail.value;
				if (tmpStr.length < 13) return;
				tmpStr = tmpStr.substr(tmpStr.length - 13);
				// TODO 接口请求
				caseService
					.searchByCode(tmpStr)
					.then((res) => {
						this.$modal.hideLoading();
						if (res.success && res.data && res.data.length > 0) {
							this.addFrom = 'infraredScan';
							this.scanVal = tmpStr;
							this.choiceGoodItem(res.data[0]);
						} else {
							this.$u.toast('未查询到该物品');
						}
					})
					.catch((e) => {
						this.$modal.hideLoading();
					});
				this.$nextTick(function() {
					this.hideVal = '';
				});
			},
			hideMask() {
				this.scanOtherShow = false;
				this.hideVal = '';
			},
			getCaseProperty(obj) {
				this.choiceData = obj;
				if (this.orginalCaseClueList && this.orginalCaseClueList.length > 0) {
					let tmpList = this.orginalCaseClueList;
					tmpList.forEach((itm) => {
						itm.name = itm.caseProperty;
						itm.uid = itm.casepropUuid;
					});
					this.singleList = [...tmpList];
					this.choiceSingleShow = true;
				} else {
					this.$u.toast('案由数据加载失败');
				}
			},
			// 查获数量失去焦点
			BlurQtyEvent(ev, str) {
				for (let i = 0; i < this.dataList.length; i++) {
					if (this.dataList[i].goodsRdmUuid == str) {
						try {
							if (Number(this.dataList[i].buyQty) < Number(this.dataList[i].qty) && !this.$u.test.isEmpty(this.dataList[
									i].qty) && this.dataList[i].qty != 0) {
								this.dataList[i].qty = 0;
								this.dataList[i].illegalQty = this.dataList[i].buyQty - this.dataList[i].qty;
								this.dataList[i].amt = this.dataList[i].qty * this.dataList[i].price;
								this.dataList[i].illegalAmt = this.dataList[i].illegalQty * this.dataList[i].illegalPrice;
								this.$u.toast('购进数量应该大于等于查获数量');
							}
							if (Number(this.dataList[i].qty) > 0) {
								this.dataList[i].qty = this.dataList[i].qty.replace(/\b(0+)/gi, '');
							}
							this.dataList[i].reapProfit = (this.dataList[i].illegalPrice - this.dataList[i].buyPrice) * this.dataList[
								i].illegalQty;
						} catch (err) {
							console.log(err);
						}
						return;
					}
				}
			},
			inputQtyEvent(ev, str) {
				for (let i = 0; i < this.dataList.length; i++) {
					if (this.dataList[i].goodsRdmUuid == str) {
						try {
							if (this.dataList[i].qty.indexOf('-') != -1) {
								this.dataList[i].qty = 0;
								this.dataList[i].amt = 0;
							}
							this.dataList[i].amt = this.dataList[i].qty * this.dataList[i].price;
							this.dataList[i].illegalQty = this.dataList[i].buyQty - this.dataList[i].qty;
							this.dataList[i].illegalAmt = this.dataList[i].illegalQty * this.dataList[i].illegalPrice;
							this.dataList[i].reapProfit = (this.dataList[i].illegalPrice - this.dataList[i].buyPrice) * this.dataList[
								i].illegalQty < 0 ? 0 : (this.dataList[i].illegalPrice - this.dataList[i].buyPrice) * this.dataList[
								i].illegalQty;
						} catch (err) {
							console.log(err);
						}
						return;
					}
				}
			},
			inputBuyQtyEvent(ev, str) {
				for (let i = 0; i < this.dataList.length; i++) {
					if (this.dataList[i].goodsRdmUuid == str) {
						try {
							this.dataList[i].illegalAmt = this.dataList[i].illegalQty * this.dataList[i].illegalPrice;
							this.dataList[i].reapProfit = (this.dataList[i].illegalPrice - this.dataList[i].buyPrice) * this.dataList[
								i].illegalQty < 0 ? 0 : (this.dataList[i].illegalPrice - this.dataList[i].buyPrice) * this.dataList[
								i].illegalQty;
						} catch (err) {
							console.log(err);
						}
						return;
					}
				}
			},
			inputBuyPriceEvent(ev, str) {
				for (let i = 0; i < this.dataList.length; i++) {
					if (this.dataList[i].goodsRdmUuid == str) {
						try {
							if (this.dataList[i].illegalPrice.indexOf('-') != -1) {
								this.dataList[i].illegalPrice = 0;
							}
							this.dataList[i].illegalAmt = this.dataList[i].illegalQty * this.dataList[i].illegalPrice;

							this.dataList[i].reapProfit = (this.dataList[i].illegalPrice - this.dataList[i].buyPrice) * this.dataList[
								i].illegalQty < 0 ? 0 : (this.dataList[i].illegalPrice - this.dataList[i].buyPrice) * this.dataList[
								i].illegalQty;
						} catch (err) {
							console.log(err);
						}
						return;
					}
				}
			},
			inputPriceEvent(ev, str) {
				for (let i = 0; i < this.dataList.length; i++) {
					if (this.dataList[i].goodsRdmUuid == str) {
						try {
							if (this.dataList[i].buyPrice.indexOf('-') != -1) {
								this.dataList[i].buyPrice = 0;
							}
							this.dataList[i].illegalbuyamt = this.dataList[i].buyQty * this.dataList[i].buyPrice;
							this.dataList[i].reapProfit = ((this.dataList[i].illegalPrice - this.dataList[i].buyPrice) * this.dataList[
								i].illegalQty) <= 0 ? 0 : (this.dataList[i].illegalPrice - this.dataList[i].buyPrice) * this.dataList[
								i].illegalQty;
						} catch (err) {
							console.log(err);
						}
						return;
					}
				}
			},
			handleBlurQty(ev, str) {
				console.log(1111)
				for (let i = 0; i < this.dataList.length; i++) {
					if (this.dataList[i].goodsRdmUuid == str) {
						try {
							if (Number(this.dataList[i].buyQty) < Number(this.dataList[i].qty) && !this.$u.test.isEmpty(this.dataList[
									i].buyQty)) {
								this.dataList[i].buyQty = 0;
								this.dataList[i].illegalAmt = 0;
								this.dataList[i].illegalQty = this.dataList[i].buyQty - this.dataList[i].qty;
								this.dataList[i].illegalbuyamt = this.dataList[i].buyQty * this.dataList[i].buyPrice;
								this.$u.toast('购进数量应该大于等于查获数量');
							}
							if (Number(this.dataList[i].buyQty) > 0) {
								this.dataList[i].buyQty = this.dataList[i].buyQty.replace(/\b(0+)/gi, '');
							}
							// 这里有问题 明天改
							this.dataList[i].reapProfit = (this.dataList[i].illegalPrice - this.dataList[i].buyPrice) * this
								.dataList[
									i].illegalQty;
						} catch (err) {
							console.log(err);
						}
						return;
					}
				}
			},
			inputGqtyEvent(ev, str) {
				for (let i = 0; i < this.dataList.length; i++) {
					if (this.dataList[i].goodsRdmUuid == str) {
						try {
							if (this.dataList[i].buyQty.indexOf('-') != -1) {
								this.dataList[i].buyQty = 0;
								this.dataList[i].illegalbuyamt = 0;
							}
							this.dataList[i].illegalbuyamt = this.dataList[i].buyQty * this.dataList[i].buyPrice;
							this.dataList[i].illegalQty = this.dataList[i].buyQty - this.dataList[i].qty;
							this.dataList[i].illegalAmt = this.dataList[i].illegalQty * this.dataList[i].illegalPrice;
						} catch (err) {
							console.log(err);
						}
						return;
					}
				}
			},

			choiceGoodItem(obj) {
				this.tmpChoice = undefined;
				this.searchFlag = false;
				this.searchTxt = '';
				if (!this.showSoftInputView) {
					this.showSearchGoods = false;
				} else {
					this.tmpChoice = obj;
					return;
				}
				if (this.preObj && this.hasMustPreIsEmpty()) {
					return;
				}
				this.$nextTick(function() {
					this.addNewItem(obj);
				});
			},
			updateDataContent(e) {
				for (let i = 0; i < this.dataList.length; i++) {
					if (this.dataList[i].goodsRdmUuid == e.goodsRdmUuid) {
						this.dataList[i].qty += 1;
						this.dataList[i].amt = this.dataList[i].qty * this.dataList[i].price;
						break;
					}
				}
			},
			addNewItem(obj) {
				if (this.realDataList.length > 0) {
					this.updatePreObj(this.realDataList[0]);
				} else {
					if (this.dataList.length > 0) {
						this.updatePreObj(this.dataList[this.dataList.length - 1]);
					}
				}
				let tmp = {};
				if (this.preObj) tmp = this.$u.deepClone(this.preObj);
				tmp.goodsRdmUuid = uuidv4(); // 前端页面用到
				tmp.goodsTypeName = this.getDicName(this.$u.dic.DIC_KEYS.CIGA, obj.productType);
				tmp.goodsType = obj.productType;
				tmp.goodsUuid = obj.productUuid;
				tmp.goodsName = obj.productName;
				tmp.unit = obj.defaultUnitName;
				tmp.price = obj.rtlPr;
				tmp.wholeSalePrice = obj.provinceWholeSalePrice;
				tmp.qty = 1;
				tmp.amt = '';
				tmp.buyPrice = '';
				tmp.reapProfit = 0;
				tmp.buyQty = 1;
				tmp.illegalbuyamt = '';
				tmp.illegalQty = '';
				tmp.illegalPrice = '';
				tmp.illegalAmt = '';
				tmp.isSearched = false;
				if (this.addFrom == 'scanBar' || this.addFrom == 'infraredScan') {
					if (this.scanVal != undefined && this.scanVal == obj.packCode) {
						tmp.qty = 0.1; // 包码 返回0.1条
					} else if (this.scanVal != undefined && this.scanVal == obj.partCode) {
						tmp.qty = 50; // 一件=50条
					} else {
						// 条码
						tmp.qty = 1;
					}
				} else {
					tmp.qty = 1;
				}

				try {
					let n = tmp.qty * tmp.price;
					tmp.amt = Number(Number(n).toFixed(2));
				} catch (e) {
					tmp.amt = 0;
				}
				this.dataList.push(tmp);
				this.$refs['mescrollRef0'].resetList(false);
			},
			getDicName(keyword, code) {
				return this.$u.dic.getDicNameByKeyCode(keyword, code);
			},
			getSearchGoodList() {
				if (this.$u.test.isEmpty(this.searchTxt)) return;
				caseService.searchPro(this.$u.test.isEmpty(this.searchTxt) ? undefined : this.searchTxt, 1, 20).then((res) => {
					if (res.success) {
						this.addFrom = 'search';
						this.scanVal = undefined;
						if (res.data.length == 1) {
							this.choiceGoodItem(res.data[0]);
						} else if (res.data.length > 1) {
							this.searchGoodList = [...res.data];
							this.searchFlag = false;
							this.showSearchGoods = true;
						}
					}
				});
			},
			hasMustPreIsEmpty() {
				if (this.$u.test.isEmpty(this.preObj.qty) || this.$u.test.isEmpty(this.preObj.propSubtype)) {
					this.$modal.alert('请完善上一条物品的信息, 便于快速录入物品', () => {});
					return true;
				}
				return false;
			},
			confirm: function() {
				if (this.realDataList.length > 0) {
					for (let i = 0; i < this.realDataList.length; i++) {
						let tmp = this.realDataList[i];
						if (this.$u.test.isEmpty(tmp.propSubtype) || this.$u.test.isEmpty(tmp.qty)) {
							this.$modal.alert(`【数据${this.realDataList.length - i} ${tmp.goodsName}】记录有数据为空？`);
							return;
						}
					}
					this.saveGoods();
				} else {
					this.$u.toast('请先添加物品！');
					return;
				}
			},
			saveGoods() {
				this.$modal.showLoading('正在保存...');
				officeService
					.saveCaseGoodsPunishList(this.caseUuid, this.dataList)
					.then((res) => {
						this.$modal.hideLoading();
						if (res.success) {
							// this.$u.toast('保存成功');
							this.backPrev({
								paramKey: 'data',
								param: this.dataList,
							});
						} else {
							this.$modal.alert('保存失败:' + res.msg);
						}
					})
					.catch((err) => {
						this.$modal.hideLoading();
						this.$modal.alert('保存失败:' + err);
					});
			},
			loadData(page, isNeedRefresh, isSplice) {
				return new Promise((resolve, reject) => {
					if (this.realDataList.length > page.size * (page.num - 1)) {
						let s = page.size * (page.num - 1);
						let len = this.realDataList.length - s;
						if (len > page.size) {
							resolve({
								data: this.realDataList.slice(s, s + page.size),
								totalCount: this.realDataList.length,
							});
						} else {
							if (isSplice) {
								// 主动拼接上一页的数据 补足limit
								let lep = page.size - (this.realDataList.length - s);
								resolve({
									data: [...this.realDataList.slice(s - lep, s), ...this.realDataList.slice(s)],
									totalCount: this.realDataList.length,
									isSplice: isSplice,
								});
							} else {
								resolve({
									data: this.realDataList.slice(s),
									totalCount: this.realDataList.length,
								});
							}
						}
					} else {
						resolve({
							data: [],
							totalCount: 0,
						});
					}
				});
			},
			dicSelectOpen: function(keyWord) {
				if (this.dicKeyWords == keyWord && this.dicShow) {
					return true;
				} else if (this.dicKeyWords == keyWord && this.dicShow) {
					return true;
				} else if (this.dicKeyWords == keyWord && this.dicShow) {
					return true;
				}
				return false;
			},
			showDic: function(key, cur, id) {
				if (this.dicShow) return;
				this.dicKeyWords = key;
				this.dicCurrentSelect = cur;
				this.dicShow = true;
				this.currIndex = id;
			},
			selectDicConfirm: function(e) {
				let obj;
				let tmpList = this.realDataList.filter((itm) => {
					if (itm.goodsRdmUuid == this.currIndex) return true;
					return false;
				});
				if (tmpList.length > 0) {
					obj = tmpList[0];
				}
				if (obj == undefined) return;
				if (this.dicKeyWords == this.$u.dic.DIC_KEYS.GOODS_PROP_SUBTYPE) {
					obj.propSubTypeName = e[0].label;
					obj.propSubtype = e[0].extra.bcbCode;
				}
			},
		},
	};
</script>

<style lang="scss" scoped>
	@import '../case_common.scss';

	.page {
		padding: 0;
	}

	uni-page-body {
		height: 100%;
	}

	.sub-title-lay {
		height: 60px;

		.sub-title {
			font-size: 0.65rem;
			color: $u-tips-color;
		}

		text {
			font-size: 1.2rem;
			font-weight: 500;
		}
	}

	.search-lay {
		height: 50px;

		.search-title {
			font-size: 0.85rem;
			font-weight: 500;
		}
	}

	.flat-bottom {
		position: fixed;
		bottom: 0;
		margin: auto;
		z-index: 88;
		height: 180rpx;
		left: 0;
		right: 0;
		background-color: white;
	}

	.flat-line {
		margin-top: 10rpx;
		margin-bottom: 10rpx;
	}

	.num-lay {
		height: 40px;
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		align-items: center;
		margin-top: 10px;
	}

	.num-title {
		border-radius: 60px;
		background-color: $u_border_color;
		padding: 8rpx 20rpx;
		margin: 15rpx;
	}

	.num-content {
		color: #222;
		font-size: 0.8rem;
		font-weight: bold;
	}

	.pty-lay {
		display: flex;
		flex-direction: row;
		align-items: center;
		padding: 10rpx 10rpx;
		margin: 0rpx 20rpx;
		border-bottom: 1px solid $u-border-color;
	}

	.price-lay {
		display: flex;
		flex-direction: row;
		align-items: center;
		padding: 10rpx 10rpx;
		margin: 0rpx 10rpx;
		border-bottom: 1px solid $u-border-color;
	}

	.divider-m {
		height: 15rpx;
		background-color: $u-border-color;
	}

	.pop-content {
		padding: 24rpx;
		touch-action: none;
	}

	.m-input {
		margin: 0 10rpx;
	}

	.pop-item {
		font-size: 14px;
		padding: 10px 10px;
		border-bottom: 1rpx solid #eeeeee;
	}

	.good-search-lay {
		width: 100%;
		margin-left: -30rpx;
		z-index: 199;
		position: absolute;
		background-color: white;
	}

	.warp {
		font-size: 40rpx;
		color: white;
		font-weight: bold;
		display: flex;
		justify-content: center;
		align-self: center;
		height: 100%;
		flex-direction: column;
	}
</style>